﻿using System;
using System.Data;
using DotNetNuke.Entities.Content;
using DotNetNuke.Entities.Portals;
using DotNetNuke.Entities.Users;

namespace DotNetNuke.Modules._2DoTasks.Components.Entities
{
    public class _TaskSubTask : ContentItem
    {
        public int Id { get; set; }

        public int ParentTaskId { get; set; }

        public int PortalId
        {
            get
            {
                return PortalController.GetCurrentPortalSettings().PortalId;
            }
        }

        public string Name { get; set; }

        public Nullable<int> AssignedUserId { get; set; }

        public Nullable<DateTime> CompletionDate { get; set; }

        public DateTime Creationdate { get; set; }

        public int CreationUserId { get; set; }

        public DateTime LastChangeDate { get; set; }

        public int LastChangeUserId { get; set; }

        public int Order { get; set; }

        /// <summary>
        /// The creation user display name
        /// </summary>
        public UserInfo CreatedByUser
        {
            get
            {
                return CreatedByUserID != 0 ? UserController.GetUserById(PortalId, CreatedByUserID) : null;
            }
        }

        /// <summary>
        /// the last change user display name
        /// </summary>
        public UserInfo LastChangeUser
        {
            get
            {
                return LastChangeUserId != 0 ? UserController.GetUserById(PortalId, LastChangeUserId) : null;
            }
        }

        /// <summary>
        /// The user that to wich the task is assigned
        /// </summary>
        public UserInfo AssignedUser
        {
            get
            {
                return AssignedUserId.HasValue ? UserController.GetUserById(PortalId, AssignedUserId.Value) : null;
            }
        }

        #region IHydratable Implementation

        public override void Fill(IDataReader dr)
        {
            Id = Convert.ToInt32(dr["STsk_TaskId"]);

            ParentTaskId = Convert.ToInt32(dr["STsk_ParentTaskId"]);

            Name = dr["STsk_Name"].ToString();

            AssignedUserId = dr["STsk_AssignedUserId"] as Nullable<int>;

            CompletionDate = dr["STsk_CompletionDate"] as Nullable<DateTime>;

            Creationdate = Convert.ToDateTime(dr["STsk_Creationdate"]);

            CreationUserId = Convert.ToInt32(dr["STsk_CreationUserId"]);

            LastChangeDate = Convert.ToDateTime(dr["STsk_LastChangeDate"]);

            LastChangeUserId = Convert.ToInt32(dr["STsk_LastChangeUserId"]);

            Order = Convert.ToInt32(dr["STsk_Order"]);

            //base.FillInternal(dr); ToDo - Implement Taxonomy
        }

        public override int KeyID
        {
            get
            {
                return Id;
            }
            set
            {
                Id = value;
            }
        }

        #endregion IHydratable Implementation
    }
}